<?php
require('FacturaVenta.class.php');
require('PresentacionP.class.php');

$modo = (isset($_GET['modo'])) ? $_GET['modo']:0;
switch($modo){
    case 1: //Consultar Grid
        if(isset($_GET['modo']) == 1){
            $page           = $_POST['page'];
            $rp             = $_POST['rp'];
            $sortname       = $_POST['sortname'];
            $sortorder      = $_POST['sortorder'];
            $query          = $_POST['query'];
            $letter_pressed = isset($_POST['letter_pressed'])?$_POST['letter_pressed']:null;
            $qtype          = $_POST['qtype'];
            $obj            = new Producto();
            $obj->consultar_grid($page, $rp, $sortname, $sortorder, $query, $letter_pressed, $qtype);
        }
        break;
    case 3: //Crear Factura Venta
        if(isset($_POST['submit'])){
            $fveId      = $_POST['id'];
            $cliId      = $_POST['cliId'];
            $usuId      = $_POST['usuId'];
            $nroFactura = $_POST['nroFactura'];
            $nroControl = $_POST['nroControl'];
            $ciudad     = $_POST['ciudad'];
            $tmpViaje   = ( strlen($_POST['tmpViaje'])<1)?0:$_POST['tmpViaje'];
            $holgura    = (strlen($_POST['holgura'])<1)?0:$_POST['holgura'];
            $tmpEntrega = (strlen($_POST['tmpEntrega'])<1)?0:$_POST['tmpEntrega'];
            
            // idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+iocId+#imp+#precio; |        
            $odc = htmlspecialchars(trim($_POST['elementos']));
            
            $itemOdc = explode("|", $odc);

            for ($i = 0, $count = count($itemOdc); $i < $count; $i++) {
                if(strlen(trim($itemOdc[$i])) >0){
                    $arrayIFV[$i] = (explode("#", $itemOdc[$i]));
                }				
            }
            
            $array[0] = $cliId;
            $array[1] = $nroFactura;
            $array[2] = $nroControl;
            $array[3] = $ciudad;
            $array[4] = $tmpViaje;
            $array[5] = $holgura;
            $array[6] = $tmpEntrega;
            $array[7] = $arrayIFV;
            $array[8] = $fveId;
            $array[9] = date("Ymd");
            $array[10] = date("His");
            $array[11] = $usuId;
            
            //print_r($array);
	    $obj    = new FacturaVenta();
            
            if ( $obj->crearFacturaVenta($array) == true)  echo 1;
            else echo 0;
        }
        break;
    case 4://Eliminar
        if(isset($_POST['submit'])){
            $id = $_POST['id'];
            $obj    = new FacturaVenta();
            if ( $obj->eliminar($id) == true) echo 1;
            else echo 0;
        }             
        break;	
    default:
        echo "Debe seleccionar un modo: \n1. Consultar Grid\n2. Insertar\n3. Modificar\n4. Eliminar";
}
?>
